<template>
  <div class="item">
    <div class="info" @click="openDetail(curItem.liveid, 'activity')">
      <div class="brand-img">
        <img alt="" v-lazy="curItem.pinpaiurl" />
      </div>
      <div class="info_r">
        <div class="info_r_top">
          <div style="display: flex; align-items: center">
            <span class="brand-name">{{ curItem.pinpaiming }}</span>
            <span class="marking" v-if="curItem.promotionLabel">{{
              curItem.promotionLabel.labelName
            }}</span>
            <span
              class="marking"
              v-if="curItem.rewards && curItem.rewards.length > 0"
              >满返</span
            >
            <span class="marking" v-if="isShowBao">包邮</span>
          </div>
          <count-time
            :endTime="
              tabType == 2 ? curItem.endtimestamp : curItem.begintimestamp
            "
            :tab-type="tabType"
          />
        </div>
        <div class="info_r_bot">
          <div class="info_sheng">
            <p class="info_sheng_l">最高省赚</p>
            <p class="info_sheng_r">
              <span>￥</span
              ><span class="money ftmDIN">{{ curItem.maxEarnMoney / 100 }}</span
              ><span>/件</span>
            </p>
          </div>
          <p class="info_trans" @click.stop="trans(curItem.liveid)">
            转发赚大钱
          </p>
        </div>
      </div>
    </div>
    <div
      style="height:2.32rem; overflow:hidden;"
      v-if="curItem.productList && curItem.productList.length > 0"
    >
      <div class="imgs" ref="imgscroll">
        <ul>
          <li
            class="itemimgs"
            v-for="(prodItem, index) in curItem.productList"
            :key="prodItem.id"
            @click.stop="openPage(index, curItem, prodItem)"
          >
            <img alt="" class="mainimg" v-lazy="prodItem.pic" />
            <img
              src="@/assets/redpack/playicon.png"
              alt=""
              class="playicon"
              v-if="index == 0 && curItem.tvRoom && curItem.tvRoom.status == 1"
            />
            <div
              class="zbinfo"
              v-if="index == 0 && curItem.tvRoom && curItem.tvRoom.status == 1"
            >
              <div class="review-label">直播中</div>
              <div class="review-num">
                {{ curItem.tvRoom.replayReviewNum }}观看
              </div>
            </div>
            <p v-if="prodItem.stock <= 30 && prodItem.stock > 0">
              库存{{ prodItem.stock }}
            </p>
            <div
              class="salled"
              v-if="prodItem.stock == 0 && curItem.statu != 1"
            >
              售罄
            </div>
          </li>
          <li class="more" @click.stop="openDetail(curItem.liveid, 'product')">
            <div>
              <span>查看全部</span>
              <van-icon name="arrow" size="9px" />
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import CountTime from './CountTime.vue';

const throttle = (fn, delay) => {
  let timer = null;
  return function() {
    const context = this;
    const args = arguments;
    clearTimeout(timer);
    timer = setTimeout(() => {
      fn.apply(context, args);
    }, delay);
  };
};

export default {
  name: 'ListItem',
  props: {
    curItem: {
      type: Object,
      default: () => {}
    },
    index: {
      type: Number,
      default: 0
    },
    tabType: {
      type: Number,
      default: 2
    }
  },
  components: {
    CountTime
  },
  data() {
    return {
      scrollLeft: 0,
      oldScrollLeft: 0,
      imgWidth: 0,
      throttleFn: ''
    };
  },
  computed: {
    tabName() {
      return this.tabType === 2
        ? '今日转发得红包'
        : this.tabType === 1
        ? '明日转发得红包'
        : '';
    },
    isShowBao() {
      let { promotionLabel, rewards, postageflag } = this.curItem;
      promotionLabel = promotionLabel || {};
      rewards = rewards || [];
      return promotionLabel.labelName && rewards.length > 0
        ? false
        : postageflag === 1;
    }
  },
  methods: {
    handleScroll() {
      const { scrollLeft } = this.$refs.imgscroll;
      this.scrollLeft = scrollLeft;
    },
    trans() {
      // todo
    },
    openPage() {
      // todo
    },
    openDetail() {
      // todo
    }
  },
  watch: {
    scrollLeft(newValue, oldValue) {
      setTimeout(() => {
        if (newValue === this.$refs.imgscroll.scrollLeft) {
          console.log('横向滚动结束');
          this.oldScrollLeft = newValue;
          const scrollLeftAll = this.$refs.imgscroll.scrollLeft;
          const img = this.$refs.imgscroll.querySelectorAll('.itemimgs');
          // 已滚动高度+可视区高度
          const outw =
            scrollLeftAll +
            this.$refs.imgscroll.querySelectorAll('ul')[0].clientWidth +
            24;
          for (let i = 0; i < img.length; i++) {
            const imgScrollLeft = img[i].offsetLeft;
            if (scrollLeftAll < imgScrollLeft && imgScrollLeft < outw) {
              // 在可视区内则显示图片
              console.log(
                this.tabName,
                this.curItem.liveid,
                this.curItem.productList[i].id
              );
            }
          }
        }
      }, 100);
      if (this.oldScrollLeft === oldValue) {
        console.log('横向滚动开始');
      }
    }
  },
  mounted() {
    if (this.$refs.imgscroll) {
      this.throttleFn = throttle(this.handleScroll, 500);
      this.$refs.imgscroll.addEventListener('scroll', this.throttleFn);
    }
    const imgDom = document.querySelector('.itemimgs');
    this.imgWidth = imgDom ? imgDom.clientWidth : 0;
  },
  destroyed() {
    if (this.$refs.imgscroll) {
      this.$refs.imgscroll.removeEventListener('scroll', this.throttleFn);
    }
  }
};
</script>

<style lang="scss" scoped>
.item {
  background: #fff;
  width: 100%;
  border-radius: 0.12rem 0 0 0.12rem;
  border: 1px solid #f0f0f0;
  padding: 0.12rem 0.12rem 0;
  box-sizing: border-box;
  overflow: hidden;
  margin-top: 0.12rem;
  margin-left: 0.08rem;

  .info {
    display: flex;
    margin-bottom: 0.24rem;

    .brand-img {
      width: 0.8rem;
      height: 0.8rem;
      border-radius: 0.04rem;
      border: 1px solid #f0f0f0;
      margin-right: 0.16rem;
      box-sizing: border-box;
      padding: 1px;
      display: flex;
      justify-content: center;
      align-items: center;

      img {
        width: 100%;
        height: 100%;
        object-fit: contain;
      }
    }

    .info_r {
      width: 100%;
      flex: 1;
    }

    .info_r_top {
      display: flex;
      margin-bottom: 0.06rem;
      align-items: center;
      justify-content: space-between;
    }

    .info_r_bot {
      overflow: hidden;

      .info_star {
        margin: 0.09rem 0.08rem 0 0;
        float: left;
        width: 1.2rem;
      }
    }

    .brand-name {
      max-width: 2.16rem;
      color: #333;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      font-size: 0.28rem;
      font-weight: 500;
    }

    .marking {
      display: flex;
      background: linear-gradient(
        135deg,
        rgba(255, 88, 94, 1) 0%,
        rgba(255, 42, 49, 1) 100%
      );
      border-radius: 2px;
      font-size: 0.2rem;
      color: #fff;
      padding: 0 2px;
      margin-left: 2px;
      height: 0.28rem;
      align-items: center;
    }

    .img-marking {
      display: inline-block;
      width: 0.52rem;
      height: 0.24rem;
      margin: 0.06rem 0 0 0.08rem;
    }

    .info_sheng {
      float: left;
      height: 0.3rem;
      line-height: 0.3rem;
      box-sizing: border-box;
      font-size: 0.24rem;
      border-radius: 0.04rem;
      overflow: hidden;
      color: #fff;
      margin-top: 0.04rem;
      float: left;
      border: 1px solid #ff3037;

      p {
        float: left;
        text-align: center;
      }

      .info_sheng_l {
        width: 1.08rem;
        height: 100%;
        background: #ff3037;
      }

      .info_sheng_r {
        color: #ff3037;
        padding-right: 0.04rem;
      }
    }

    .info_trans {
      float: right;
      box-sizing: border-box;
      border-radius: 0.28rem;
      width: 1.68rem;
      padding: 0.02rem 0;
      color: #ff3037;
      font-size: 0.24rem;
      text-align: center;
      border: 1px solid #ff3037;
    }
  }

  .imgs {
    white-space: nowrap;
    width: 100%;
    height: 2.5rem;
    overflow-x: auto;
    overflow-y: hidden;
    text-align: left;
    -webkit-overflow-scrolling: touch; /* 用于 ios5+ */
    li {
      position: relative;
      width: 2.2rem;
      height: 2.2rem;
      border-radius: 0.04rem;
      overflow: hidden;
      background: url('../../../assets/redpack/defaultbg.png') no-repeat;
      background-size: 100%;
      margin-right: 0.08rem;
      display: inline-block;

      .salled {
        width: 1.04rem;
        height: 1.04rem;
        line-height: 1.04rem;
        text-align: center;
        background: rgba(0, 0, 0, 0.697);
        border-radius: 50%;
        color: #fff;
        font-size: 0.3rem;
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        margin: auto;
      }

      .mainimg {
        width: 2.2rem;
        height: 2.2rem;
        display: block;
        border: none;
      }

      .playicon {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        width: 0.6rem;
        height: 0.6rem;
        margin: auto;
        z-index: 10;
      }

      .zbinfo {
        font-size: 0.2rem;
        color: #fff;
        position: absolute;
        bottom: 0.1rem;
        left: 0px;
        display: flex;

        .review-label {
          width: 0.76rem;
          height: 0.38rem;
          background: linear-gradient(
            135deg,
            rgba(255, 88, 94, 1) 0%,
            rgba(255, 48, 55, 1) 100%
          );
          border-radius: 0.06rem 0 0 0.06rem;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .review-num {
          /*width: 1.16rem;*/
          height: 0.38rem;
          float: left;
          background: #393e4e;
          opacity: 0.5;
          border-radius: 0 0.06rem 0.06rem 0;
          display: flex;
          padding: 0 0.06rem;
          align-items: center;
          overflow: hidden;
        }
      }

      p {
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 0.32rem;
        background: #fff;
        opacity: 0.79;
        color: #333;
        font-size: 0.18rem;
        text-align: center;
        /*border-radius:0px 0px 2px 2px;*/
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }

    .more {
      text-align: center;
      background: #f0f0f0;
      color: #666;
      font-size: 0.28rem;
      vertical-align: top;
      position: relative;

      div {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        line-height: 0.4rem;
        margin-top: 0.94rem;

        span {
          margin-right: 0.08rem;
        }
      }
    }
  }
}
</style>
